const express = require('express');
const mongoose = require('mongoose');
const { AuthRoute } = require('./auth');
const { HttpAuthException } = require('./util/HttpException');
const { status } = require('./util/status');

mongoose.connect('mongodb://localhost/map');

const app = express();

app.use('*', (req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTION');
    res.header('Access-Control-Allow-Headers', 'Content-type');
    next();
})
app.use(express.json());

app.use(AuthRoute);



app.use((err, req, res, next) => {
    if (err) {
        if (err instanceof HttpAuthException) {
            res.status(status.AUTH_EXCEPTION).send(err);
            return ;
        }
    }
    next();
})

app.listen(10025);
